package com.wjx.demo.lamba

fun <T> max(list: ArrayList<T>, less: (T, T) -> Boolean): T? {
    var max: T? = null
    for (it in list) {
        if (max == null || less(max, it)) {
            max = it
        }
    }
    return max
}

fun <T, R> List<T>.map(transform: (T) -> R): List<R> {
    var result = arrayListOf<R>()
    for (item in this) {
        result.add(transform(item))
    }
    return result
}

fun sum(a: Int, b: Int): Int = a + b


fun isOdd(i: Int) = i % 2 != 0

fun main(args: Array<String>) {
    var list: ArrayList<Int> = arrayListOf(1, 4, 3, 7)
//    list.max(list,{a:Int,b:Int->a>b})
    list.filter(::isOdd).forEach { print(it) }
    println()
    var list1 = list.map { value -> value * 2 }
    list1.forEach { print(it) }
    println()
    val double = fun Int.(other: Int): Int = this + other * 2
    println(1.double(1))


}